package com.host.dyyl;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.host.dyyl.utils.MyHttpUtils;
import com.host.dyyl.view.ProgressWebView;
import com.jaeger.library.StatusBarUtil;


import org.json.JSONObject;
import java.net.URLEncoder;

/**
 * check应用名、lianAddress
 */
public class MainActivity3 extends Activity implements View.OnClickListener {

    private TextView back_tv, title_tv, reload_tv;
    private ProgressWebView main_wv;
    private TextView to_home_tv, to_back_tv, to_refresh_tv, to_clear_tv;
    private View cover_view;
    private ImageView welcome_iv, bottom_back_iv;
    private RelativeLayout xufei_layout;
    private final static int FILECHOOSER_RESULTCODE = 1;

    private MyHandler myHandler = new MyHandler();

    private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    xufei_layout.setVisibility(View.VISIBLE);
//                    showAlert();
                    break;
                case 1:
                    String url = (String) msg.obj;
                    main_wv.loadUrl(url);
//                    checkCameraPermission();
                    break;
                case 2:
//                    main_wv.loadUrl(UrlConstant.IP);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        setContentView(R.layout.activity_main3);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.title_bg), 0);
        init();
//        check();
        checkLianBaseAddress();
//        main_wv.loadUrl(UrlConstant.IP);
//        checkMyBaseAddress();
    }

    private void init() {
        back_tv = findViewById(R.id.back_tv);
        back_tv.setOnClickListener(this);
        reload_tv = findViewById(R.id.reload_tv);
        reload_tv.setOnClickListener(this);
        title_tv = findViewById(R.id.title_tv);
        main_wv = findViewById(R.id.main_wv);

        to_home_tv = findViewById(R.id.to_home_tv);
        to_home_tv.setOnClickListener(this);
        to_back_tv = findViewById(R.id.to_back_tv);
        to_back_tv.setOnClickListener(this);
        to_refresh_tv = findViewById(R.id.to_refresh_tv);
        to_refresh_tv.setOnClickListener(this);
        to_clear_tv = findViewById(R.id.to_clear_tv);
        to_clear_tv.setOnClickListener(this);

        xufei_layout = findViewById(R.id.xufei_layout);
        cover_view = findViewById(R.id.cover_view);
        welcome_iv = findViewById(R.id.welcome_iv);
        bottom_back_iv = findViewById(R.id.bottom_back_iv);
        bottom_back_iv.setOnClickListener(this);

        initWelcome();
        initWebView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (main_wv != null) {
            main_wv.onResume();
//有的5.0的机型只显示一次选择图片的对话框  解决办法是加上这句
            if (main_wv.mUploadCallbackAboveL != null) {
                main_wv.mUploadCallbackAboveL.onReceiveValue(null);
            }
        }
    }

    private void initWebView() {

        main_wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                for (int i = 0; i < StringConstant.jumpStrs.length; i++) {
                    if (!TextUtils.isEmpty(url)
                            && url.contains(StringConstant.jumpStrs[i])) {
                        Uri uri = Uri.parse(url);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);
                        return true;
                    }
                }

                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
                super.onReceivedSslError(view, handler, error);
            }


        });
//        main_wv.loadUrl("https://www.baidu.com");
        main_wv.setOnRefreshTitleListener(new ProgressWebView.OnRefreshTitleListener() {
            @Override
            public void refresh(String title) {
                title_tv.setText(title);
            }
        });
    }

    private void initWelcome() {
        welcome_iv.setImageResource(R.mipmap.welcome_bg);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(welcome_iv, "alpha", 0.6f, 1.0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(3000);
        animatorSet.play(objectAnimator);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                cover_view.setVisibility(View.GONE);
                welcome_iv.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        animatorSet.start();
    }


    public String getAppName(Context context, String packname){
        //包管理操作管理类
        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo info = pm.getApplicationInfo(packname, 0);
            return info.loadLabel(pm).toString();
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return packname;
    }
    private void checkLianBaseAddress() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String appName =getString(R.string.app_name);

                    String s = URLEncoder.encode(appName, "utf-8");
                    String result = MyHttpUtils.toGet(UrlConstant.MINGYANG_CHECK_IP + "?name=" + s);
                    if (!TextUtils.isEmpty(result)) {
                        JSONObject jsonObject = new JSONObject(result);
                        String b = jsonObject.optString("url");
                        if (!TextUtils.isEmpty(b)) {
                            Message message = myHandler.obtainMessage();
                            message.what = 1;
                            message.obj = b;
                            myHandler.sendMessage(message);
                        } else {
                            myHandler.sendEmptyMessage(2);
                        }
                    } else {
                        myHandler.sendEmptyMessage(2);
                    }
                } catch (Exception e) {

                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_tv:
                if (main_wv.canGoBack()) {
                    main_wv.goBack(); //goBack()表示返回WebView的上一页面
                }
                break;
            case R.id.bottom_back_iv:
                if (main_wv.canGoBack()) {
                    main_wv.goBack(); //goBack()表示返回WebView的上一页面
                }
                break;
            case R.id.reload_tv:
                main_wv.reload();
                break;
            case R.id.to_home_tv:
                main_wv.loadUrl(UrlConstant.IP);
                break;
            case R.id.to_back_tv:
                if (main_wv.canGoBack()) {
                    main_wv.goBack(); //goBack()表示返回WebView的上一页面
                }
                break;
            case R.id.to_refresh_tv:
                main_wv.reload();
                break;
            case R.id.to_clear_tv:
                Toast.makeText(MainActivity3.this, "清除成功", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

}
